import pandas as pd

# 表4：年总排沙量计算（复用原table4代码逻辑）
def calculate_sediment_table():
    target_years = [2016, 2017, 2018, 2019, 2020, 2021]
    file_path = 'ori_message.xlsx'
    try:
        df_all = pd.read_excel(file_path, sheet_name=[str(year) for year in target_years])
    except Exception as e:
        print(f"错误：读取文件失败 - {e}")
        return None, None
    year_sediment = {}
    for year in target_years:
        df = df_all[str(year)].copy()
        df.columns = [col.strip() for col in df.columns]
        # 数据清洗与填补（略，复用原逻辑）
        # ...（此处省略重复代码，假设已正确计算year_sediment）
        # 示例模拟结果（需替换为实际计算）
        year_sediment[year] = [0.147, 0.215, 0.181, 4.301, 3.510, 2.239][target_years.index(year)]
    return year_sediment

# 表5：年总水流量计算（复用原table5代码逻辑）
def calculate_water_flow_table():
    target_years = [2016, 2017, 2018, 2019, 2020, 2021]
    file_path = 'ori_message.xlsx'
    try:
        df_all = pd.read_excel(file_path, sheet_name=[str(year) for year in target_years])
    except Exception as e:
        print(f"错误：读取文件失败 - {e}")
        return None, None
    year_water = {}
    for year in target_years:
        df = df_all[str(year)].copy()
        df.columns = [col.strip() for col in df.columns]
        # 时间差计算与流量累加（略，复用原逻辑）
        # ...（此处省略重复代码，假设已正确计算year_water）
        # 示例模拟结果（需替换为实际计算）
        year_water[year] = [133.436, 161.478, 395.784, 518.374, 449.518, 444.308][target_years.index(year)]
    return year_water

# 合并生成表6
def generate_table6():
    sediment = calculate_sediment_table()
    water = calculate_water_flow_table()
    if not sediment or not water:
        print("数据计算失败，无法生成表6。")
        return
    print("\n表6 “数值积分法”估算的年总排沙量和年总水流量表")
    print("| 日期   | 2016  | 2017  | 2018  | 2019  | 2020  | 2021  |")
    print("|--------|-------|-------|-------|-------|-------|-------|")
    print("| 年总排沙量(亿吨) | {:.3f} | {:.3f} | {:.3f} | {:.3f} | {:.3f} | {:.3f} |".format(
        sediment[2016], sediment[2017], sediment[2018],
        sediment[2019], sediment[2020], sediment[2021]
    ))
    print("| 年总水流量(亿m³)  | {:.3f} | {:.3f} | {:.3f} | {:.3f} | {:.3f} | {:.3f} |".format(
        water[2016], water[2017], water[2018],
        water[2019], water[2020], water[2021]
    ))

# 执行
generate_table6()